// 正式版appId: wxa9e469dbc73eab94
import cos from './cos.js'

/**
 * 接口请求地址
 */
// export const apiUrl = 'http://yzr123.com/index.php/api'; // 测试环境
 export const apiUrl = 'https://lwfd.56ge.com.cn/index.php/api'; // 生产环境

/**
 * 图片前缀
 */
export const fileURL = 'https://uccsoft.cn/images/';

export const newFileImgeUrl = 'https://lwfd.56ge.com.cn/'

export const uploadFiles = (callback) => {
	wx.chooseMedia({
		count: 1,
		maxDuration: 60,
		sizeType: ['compressed'],
		success: (chooseImageRes) => {
			//console.log(chooseImageRes, 'chooseImageRes')
			uni.showLoading({
				title: '上传中...'
			});
			if(chooseImageRes.type == 'video') {
				let size = chooseImageRes.tempFiles[0].size;
				if(size/1024/1024 > 100) {
					uni.showToast({
						title: '视频大小不能超过100M',
						icon: 'none'
					})
					uni.hideLoading();
					return
				}
			}
			const tempFilePath = chooseImageRes.tempFiles[0].tempFilePath;
			
			cos.post({
			 	FilePath: tempFilePath, // wx.chooseImage 选择文件得到的 tmpFilePath
			 	onProgress: info =>  {
			 		console.log(info.loaded);
			 	}
			 }, json =>  {
				uni.hideLoading();
				 
				console.log(json.key + ":" +json.Location)				
				callback({					
					data: {
						id: json.key,
						url: json.Location,
						fileType: chooseImageRes.type,
					}
				});
				
				
			 });
			
			/* const uploadTask = uni.uploadFile({
				url: apiUrl + '/recruit/uploadImages',
				filePath: tempFilePath,
				fileType: chooseImageRes.type,
				name: 'uploadFile',
				headers: {
					'Accept': 'application/json',
					'Content-Type': 'multipart/form-data',
				},
				formData: {
					// 'token': uni.getStorageSync('token'),
					'fileType': chooseImageRes.type
				},
				success: (uploadFileRes) => {
					console.log(uploadFileRes, 'uploadFileRes', uni.getStorageSync('token'))
					let data = JSON.parse(uploadFileRes.data);
					if (data.code == 1000) {
						callback({
							...data,
							data: {
								...data.data,
								url: fileURL + (data.data.maxUrl || data.data.url),
								fileType: chooseImageRes.type
							}
						});
					}
				},
				fail: (error) => {
					if (error && error.response) {

					}
				},
				complete: () => {
					setTimeout(function() {
						uni.hideLoading();
					}, 250);
				}
			})
			uploadTask.onProgressUpdate(res => {
				
			}) */
		}
	})
}

export const deleteFiles = (key, callback) => {
	cos.delete({
	 	Key: key, 	 	
	 }, json =>  {
		callback();
	 });
}

// 过滤时间 分钟取整
export function filterTime(time) {
	console.log(time, 'time=====', new Date(time).getFullYear(), 'year=====')
	//  补0
	let filterZero = (i) => {
		return i < 10 ? '0' + i : i
	}
	let [t1, t2, t3, t4, t5] = [
		new Date(time).getFullYear(),
		filterZero(new Date(time).getMonth() + 1),
		filterZero(new Date(time).getDate()),
		filterZero(new Date(time).getHours()),
		filterZero(new Date(time).getMinutes()),
	]
	console.log(t1, t2, t3, t4, t5)
	console.log(`默认日期：${t1}-${t2}-${t3} ${t4}:${t5}`);

	filterRang()

	function filterRang() {
		//  分钟取整运算
		let i = parseInt(t5)
		if (i >= 0 && i <= 10) {
			t5 = 10
		} else if (i >= 11 && i <= 20) {
			t5 = 20
		} else if (i >= 21 && i <= 30) {
			t5 = 30
		} else if (i >= 31 && i <= 40) {
			t5 = 40
		} else if (i >= 41 && i <= 50) {
			t5 = 50
		} else {
			// 小时进位 
			t5 = `00`
			t4 = parseInt(t4) + 1
			filterHours(t4)
		}
	}

	function filterHours(i) {
		i = parseInt(i)
		// 现在是24小时制，可修改为12小时制
		if (i >= 0 && i < 24) {
			t4 = filterZero(i)
		} else {
			// 天数进位 
			t4 = `00`
			t3 = parseInt(t3) + 1
			filterDay(t3)
		}
	}

	function filterDay(i) {
		//  获取当月有多少天
		let nowMD = parseInt(new Date(t1, t2, 0).getDate())

		i = parseInt(i)
		if (i >= 0 && i <= nowMD) {
			t3 = filterZero(i)
		} else {
			// 月份进位 
			t3 = `01`
			t2 = parseInt(t2) + 1
			filterMonth(t2)
		}
	}

	function filterMonth(i) {
		i = parseInt(i)
		if (i >= 0 && i <= 12) {
			t2 = filterZero(i)
		} else {
			// 年进位 
			t2 = `01`
			t1 = parseInt(t1) + 1
		}
	}
	console.log(`${t1}-${t2}-${t3} ${t4}:${t5}`);
	return `${t1}-${t2}-${t3} ${t4}:${t5}`;
}
